package com.woniu.tms.interview.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import com.woniu.tms.entity.Classes;
import com.woniu.tms.entity.ClassesExample;
import com.woniu.tms.entity.Companyinfo;
import com.woniu.tms.entity.CompanyinfoExample;
import com.woniu.tms.entity.Interview;
import com.woniu.tms.entity.InterviewExample;
import com.woniu.tms.entity.User;
import com.woniu.tms.entity.UserExample;
import com.woniu.tms.entity.UserRole;
import com.woniu.tms.entity.UserExample.Criteria;
import com.woniu.tms.interview.service.InterviewService;
import com.woniu.tms.mapper.ClassesMapper;
import com.woniu.tms.mapper.CompanyinfoMapper;
import com.woniu.tms.mapper.InterviewMapper;
import com.woniu.tms.mapper.UserMapper;
import com.woniu.tms.mapper.UserRoleMapper;
import com.woniu.tms.studentinfo.MD5Utils;
@Service
@Transactional
public class InterviewServiceImpl implements InterviewService{
@Autowired
InterviewMapper interviewMapper;
@Autowired
UserMapper userMapper;
@Autowired
CompanyinfoMapper companyinfoMapper;
@Autowired
UserRoleMapper userRoleMapper;
@Autowired
ClassesMapper classesMapper;
//给用户表中的公司添加sno
static String sno;
public String sno() {
	Date date = new Date();
	SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd");
	sno="CI"+sdf.format(date)+getRandom(4);
	return sno;
}
private static String getRandom(int n) {
	StringBuilder str = new StringBuilder();
	for (int i = 0; i < n; i++) {
		int random = (int) (Math.random() * 10);
		str.append(random);
	}
	return str.toString();
}

@Override
public List<Companyinfo> finByUnameLike1(String info) {
	List<Companyinfo> list = new ArrayList<>();
	List<Companyinfo> list2= companyinfoMapper.selectByExample(null);
	if(info!=null&&!info.trim().equals("")) {
		for (Companyinfo companyinfo : list2) {
			if(companyinfo.getCompanyUser().getUname().contains(info)) {
				list.add(companyinfo);
			}
		}
		return list;
	}
	return list2;
}

@Override
public Integer UWB() {
	UserExample example =new UserExample();
	com.woniu.tms.entity.UserExample.Criteria criteria = example.createCriteria();
	criteria.andUstatusEqualTo("未就业");
	long countByExample = userMapper.countByExample(example);
			return (int) countByExample;
}

@Override
public Integer EWB() {
	UserExample example =new UserExample();
	com.woniu.tms.entity.UserExample.Criteria criteria = example.createCriteria();;
	criteria.andUstatusEqualTo("已就业");
	long countByExample = userMapper.countByExample(example);
	return (int) countByExample;
}

//添加公司用户和公司详情
@Override
public void insert(String companyName,String uphone,String uemail,String companyInfo,User loginUser) {
	User user = new User();
	user.setSno(sno());
	user.setUname(companyName);
	user.setUeamail(uemail);
	user.setUphone(uphone);
	String passWord = MD5Utils.generate("123456");
	user.setUpwd(passWord);
	userMapper.insert(user);
	Companyinfo companyinfo = new Companyinfo();
	UserExample example = new UserExample();
	com.woniu.tms.entity.UserExample.Criteria criteria = example.createCriteria();
	criteria.andSnoEqualTo(sno);
	    List<User> list = userMapper.selectByExample(example);
	    User user2= list.get(0);
		companyinfo.setInfo(companyInfo);
		companyinfo.setCompanyid(user2.getUid());
		companyinfo.setEmpid(loginUser.getUid());
		companyinfoMapper.insertSelective(companyinfo);
		UserRole userRole = new UserRole();
		userRole.setUid(user2.getUid());
		userRole.setRid(5);
		userRoleMapper.insertSelective(userRole);
}

//删除一个公司信息
@Override
public void delOne(Integer companyid) {
	CompanyinfoExample example = new CompanyinfoExample();
	com.woniu.tms.entity.CompanyinfoExample.Criteria criteria = example.createCriteria();
	System.out.println("-----------------"+companyid);
	criteria.andCompanyidEqualTo(companyid);
	companyinfoMapper.deleteByExample(example);
	UserExample example2=new UserExample();
	Criteria criteria2 = example2.createCriteria();
	criteria2.andUidEqualTo(companyid);
	userMapper.deleteByExample(example2);
	
}

@Override
public void delAll(Integer[] companyids) {
	for (Integer integer : companyids) {
		delOne(integer);
	}
	
}

//修改公司信息
@Override
public void update(Integer companyid,String companyName,String companyInfo) {
	CompanyinfoExample example = new CompanyinfoExample();
	com.woniu.tms.entity.CompanyinfoExample.Criteria criteria = example.createCriteria();
	criteria.andCompanyidEqualTo(companyid);
	Companyinfo companyinfo = (Companyinfo) companyinfoMapper.selectByExample(example);
	companyinfo.setInfo(companyInfo);
	companyinfo.getCompanyUser().setUname(companyName);
	companyinfoMapper.updateByExample(companyinfo, example);
	
}

}
